﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using TimeTabling.Core;
using System.Data.SqlClient;
using System.Data;
using timetabling.core;
using TimeTabling.core;
using timetabling.dal;

namespace TimeTabling.DAL
{
    public class CenterDAO : GenericDAO<Center>, ICenterDAO
    {
        

        public CenterDAO(string connectionString) 
            :base(connectionString)
        {
            
        }
        
      /*  public void Add(Center center)
        {
            string command ="Insert into Center Values(@Id, @Name, @Address)";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@Id", center.Id); 
                sqlCommand.Parameters.AddWithValue("@Name", center.Name);
                sqlCommand.Parameters.AddWithValue("@Address", center.Address);
                try
                {
                    sqlConnection.Open();
                    sqlCommand.ExecuteNonQuery();
                }
                finally
                {
                    if (sqlConnection.State != ConnectionState.Closed)
                    {
                        sqlConnection.Close();
                    }
                }
            }
        }


        public Center Get(Guid id)
        {
            Center result = null;
            string command = @"Select Id, Name, Address 
                               From Center 
                               Where Id = @Id";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@Id", id);
                sqlConnection.Open();
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    if (sqlDataReader.HasRows)
                    {
                        sqlDataReader.Read();
                        result = new Center()
                        {
                            Id = (Guid)sqlDataReader["Id"],
                            Name = (string)sqlDataReader["Name"],
                            Address = (string)sqlDataReader["Address"]
                        };
                        sqlDataReader.Close();
                    }
                }
                sqlConnection.Close();
            }
            return result;
        }


        public IList<Center> GetAll()
        {
            IList<Center> resultlist = null;
            string command = @"Select Id, Name, Address
                               From Center";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                sqlConnection.Open();
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    resultlist = new List<Center>();
                   
                    while (sqlDataReader.HasRows)
                    {
                        sqlDataReader.Read();
                        resultlist.Add(new Center()
                        {
                            Id = (Guid)sqlDataReader["Id"],
                            Name = (string)sqlDataReader["Name"],
                            Address = (string)sqlDataReader["Address"]
                        });
                    }
                    sqlDataReader.Close();
                }
                sqlConnection.Close();
            }
            return resultlist;
        }


        public void Update(Center entity)
        {
            //  Genero la consulta
            string command = "UPDATE Center SET Name=@Name, Address =@Address WHERE Id = @Id";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@Id", entity.Id);
                sqlCommand.Parameters.AddWithValue("@Name", entity.Name);
                sqlCommand.Parameters.AddWithValue("@Address", entity.Address);

                try
                {
                    sqlConnection.Open();
                    sqlCommand.ExecuteNonQuery();
                }
                finally
                {
                    if (sqlConnection.State != ConnectionState.Closed)
                    {
                        sqlConnection.Close();
                    }
                }

            }


        }

        public int count()
        {
            //  Genero la consulta
            string command = "SELECT count(Id) FROM Center WHERE Id = @Id";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);

                try
                {
                    sqlConnection.Open();
                    return (int)sqlCommand.ExecuteScalar();
                }
                finally
                {
                    if (sqlConnection.State != ConnectionState.Closed)
                    {
                        sqlConnection.Close();
                    }
                }

            }
        }

        
        public void Remove(Center entity)
        {
            //  Genero la consulta
            string command = "DELETE FROM Center WHERE Id = @Id";
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommand = new SqlCommand(command, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@Id", entity.Id);

                try
                {
                    sqlConnection.Open();
                    sqlCommand.ExecuteNonQuery();
                }
                finally
                {
                    if (sqlConnection.State != ConnectionState.Closed)
                    {
                        sqlConnection.Close();
                    }
                }

            }
        }

    */
    
    
    
    }
}
